package com.bw.common.mvvm.view;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.common.field.BusAction;
import com.bw.common.field.DataActionEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;

import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

public abstract class BaseActivity<V extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity implements IView {

    protected V v;
    protected VM vm;
    private BusDataObserver busDataObserver = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        busDataObserver = new BusDataObserver(this);
        initBinding();
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getUiDataFunction().getUiData().observe(this,busDataObserver);
        v.setVariable(vrId(),vm);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        vm.getUiDataFunction().getUiData().removeObserver(busDataObserver);
        getLifecycle().removeObserver(vm);
    }

    @Override
    public void initBinding() {
        v = DataBindingUtil.setContentView(this,bindLayout());
    }

    @Override
    public void initViewModel() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz = (Class) type.getActualTypeArguments()[1];
        vm = (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory())
                .get(clazz);
    }

    //开启界面
    private void startPage(DataActionEntity entity){
        Map<String,Object>map = entity.data;
        String path = (String) map.get("activity");
        ARouter.getInstance().build(path).navigation();
    }
    //关闭界面
    private void finishPage(){
        finish();
    }
    //带参数开启界面
    private void startBundPage(DataActionEntity entity){
        Map<String,Object>map = entity.data;
        String path = (String) map.get("activity");
        Bundle bundle = (Bundle) map.get("bundle");
        ARouter.getInstance().build(path).withBundle("bundle",bundle).navigation();
    }
    //带返回值开启界面
    private void startResultPage(DataActionEntity entity){
        Map<String,Object>map = entity.data;
        String path = (String) map.get("activity");
        ARouter.getInstance().build(path).navigation(this,100);
    }
    //显示dialog
    private void showDialog(){

    }
    //关闭dialog
    private void hideDialog(){

    }
    public static class BusDataObserver implements Observer<DataActionEntity>{

        private WeakReference<BaseActivity>weakReference;

        public BusDataObserver(BaseActivity activity){
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void onChanged(DataActionEntity entity) {
            if (weakReference.get()==null)
                return;
//            LogUtils.e("weakReference:onChanged"+entity.action);
            //调用整合完成的启动界面
            if (entity.action.equals(BusAction.STARTPAGE)) {
                weakReference.get().startPage(entity);
                return;
            }
            if (entity.action.equals(BusAction.FINISHPAGE)){
//                LogUtils.e("BaseActivity:"+BusAction.FINISHPAGE);
                weakReference.get().finishPage();
                return;
            }
            if (entity.action.equals(BusAction.STARTBUNDPAGE)){
                weakReference.get().startBundPage(entity);
                return;
            }
            if (entity.action.equals(BusAction.STARTRESULTPAGE)){
                weakReference.get().startResultPage(entity);
                return;
            }
            if (entity.action.equals(BusAction.SHOWDIALOG)){
                weakReference.get().showDialog();
                return;
            }
            if (entity.action.equals(BusAction.HIDEDIALOG)){
                weakReference.get().hideDialog();
                return;
            }
        }
    }

}
